package com.chu.controller;

import com.chu.entity.Dept;
import com.chu.entity.JobPublish;
import com.chu.enums.PageEnum;
import com.chu.service.DeptService;
import com.chu.service.JobPublishService;
import com.chu.utils.ResultVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class JobPublishController {

    @Autowired
    private JobPublishService service;
    @Autowired
    private DeptService deptService;


    //职位发布的展示
    @RequestMapping("/zhaopin/demo1/jobPublish")
    @RequiresPermissions("user:jobPublish")
    public  String jobPublish(@RequestParam(defaultValue = "1")int page, String jobType,String recruitName,String registrationTime,String deadline, ModelMap map){

        PageInfo<JobPublish> pageInfo = service.findJobByRecruitType( jobType, recruitName, registrationTime, deadline, page, PageEnum.SIZE.getSize());

        map.addAttribute("jobType",jobType);
        map.addAttribute("recruitName",recruitName);
        map.addAttribute("registrationTime",registrationTime);
        map.addAttribute("deadline",deadline);
        map.addAttribute("pageInfo",pageInfo);
        return "zhaopin/demo1/list";

    }
    //职位增加的回显
    @RequestMapping("/zhaopin/demo1/checkDept")
    public String checkDept(ModelMap map){
        List<Dept> deptList = deptService.findAllDept();
        map.addAttribute("deptList",deptList);
        return "zhaopin/demo1/add";
    }
    //职位增加
    @RequestMapping("/zhaopin/demo1/saveJob")
    public String saveJob(JobPublish publish,HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        int result = service.insertSelective(publish);
        if(result>0){
            try {
                response.getWriter().write("<script>alert('增加成功！');location.href='/zhaopin/demo1/jobPublish'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write("<script>alert('增加成功！');location.href='/zhaopin/demo1/checkDept'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    //职位删除
    @RequestMapping("/zhaopin/demo1/deleteJob")
    public void deleteJob(int id, HttpServletResponse response){
        response.setContentType("application/json;charset=UTF-8");
        int result = service.deleteByPrimaryKey(id);
        ObjectMapper mapper = new ObjectMapper();
        if (result>0){
            try {
                response.getWriter().write(mapper.writeValueAsString(new ResultVO(0,"删除成功")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write(mapper.writeValueAsString(new ResultVO(13,"删除失败")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //职位修改的回显
    @RequestMapping("/zhaopin/demo1/checkOneJob")
    public String checkOneJob(int id,ModelMap map){

        JobPublish jobPublish = service.selectByPrimaryKey(id);
        map.addAttribute("jobPublish",jobPublish);
        System.out.println(jobPublish);

        List<Dept> deptList = deptService.findAllDept();
        map.addAttribute("deptList",deptList);

        return "/zhaopin/demo1/update";
    }
    //职位修改的数据封装
    @RequestMapping("/zhaopin/demo1/updateJob")
    public String updateJob(JobPublish jobPublish,HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        int result = service.updateByPrimaryKeySelective(jobPublish);
        if(result>0){
            try {
                response.getWriter().write("<script>alert('修改成功！');location.href='/zhaopin/demo1/jobPublish'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write("<script>alert('修改成功！');location.href='/zhaopin/demo1/checkOneJob'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
